package com.suray.wcs.web.system.controller;

import com.alibaba.fastjson.JSON;
import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.SurayBeanUtils;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.system.bo.ZoneRuleBO;
import com.suray.wcs.service.system.entity.ZoneRuleDB;
import com.suray.wcs.service.system.service.INodeDBService;
import com.suray.wcs.service.system.service.IZoneRuleService;
import com.suray.wcs.web.system.request.AddZoneRuleRequest;
import com.suray.wcs.web.system.request.UpdateZoneRuleRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Think
 */
@RestController
@RequestMapping("/zoneRule")
@CrossOrigin

/**
 * @author 12419
 * @title: ZoneRuleController
 * @projectName web_dev
 * @description:
 * @date 2021/1/2911:37
 */
public class ZoneRuleController extends BaseController {
    @Autowired
    private IZoneRuleService zoneRuleService;
    @Autowired
    private INodeDBService nodeDBService;

    @PreAuthorize("@ss.hasPermi('zoneRule:list')")
    @GetMapping(value = "/getZoneRuleList")
    @ResponseBody
    @ApiOperation(value = "获取区域配置列表")
    public TableDataInfo getZoneRuleList(ZoneRuleBO zoneRuleBO) {
        startPage();
        if (null == zoneRuleBO.getGridzList() || zoneRuleBO.getGridzList().size() == 0) {
            ArrayList<Integer> zList = new ArrayList<>();
            zList.add(null);
            zoneRuleBO.setGridzList(zList);
        }
        List<ZoneRuleDB> zoneRuleList = zoneRuleService.getZoneRuleList(zoneRuleBO.getGridxStart(), zoneRuleBO.getGridyStart(), zoneRuleBO.getGridzList().get(0));
        for (ZoneRuleDB zoneRuleDB : zoneRuleList) {
            zoneRuleDB.setGridzList(JSON.parseArray(zoneRuleDB.getGridz(), Integer.class));

        }
        return getDataTable(zoneRuleList);
    }

    @GetMapping(value = "/getZoneNameList")
    @ResponseBody
    @ApiOperation(value = "获取区域配置区域姓名列表")
    public ResponseData getZoneRuleDirectionList() {
        return ResponseData.success(zoneRuleService.getZoneNameList());
    }

    @PreAuthorize("@ss.hasPermi('zoneRule:add')")
    @Log(title = "区域配置管理", method = "添加区域配置", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addZoneRule")
    @ResponseBody
    @ApiOperation(value = "添加区域配置")
    public ResponseData addZoneRule(@RequestBody AddZoneRuleRequest request) {
        if (request == null || !request.checkParams()) {
            return ResponseData.error("请求参数有误");
        }
        ZoneRuleBO zoneRuleBO = SurayBeanUtils.copyProperties(request, ZoneRuleBO.class);
        if (zoneRuleService.checkZoneRuleName(zoneRuleBO)) {
            return ResponseData.error("请求参数有误,名称已存在");
        }
        zoneRuleService.addZoneRule(zoneRuleBO);
        return ResponseData.success("添加区域配置成功");
    }

    @PreAuthorize("@ss.hasPermi('zoneRule:edit')")
    @Log(title = "区域配置管理", method = "更新区域配置", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/updateZoneRule")
    @ResponseBody
    @ApiOperation(value = "更新区域配置")
    public ResponseData updateZoneRule(@RequestBody UpdateZoneRuleRequest request) {
        if (request == null || !request.checkParams()) {
            return ResponseData.error("请求参数有误");
        }
        ZoneRuleBO zoneRuleBO = SurayBeanUtils.copyProperties(request, ZoneRuleBO.class);
        zoneRuleService.updateZoneRule(zoneRuleBO);
        return ResponseData.success("更新区域配置成功");
    }

    @PreAuthorize("@ss.hasPermi('zoneRule:remove')")
    @Log(title = "区域配置管理", method = "删除区域配置", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteZoneRule/{ids}")
    @ResponseBody
    @ApiOperation(value = "删除区域配置")
    public ResponseData deleteZoneRule(@PathVariable List<Integer> ids) {
        if (ids == null || ids.size() < 1) {
            return ResponseData.error("请求参数有误");
        }
        zoneRuleService.deleteZoneRule(ids);
        return ResponseData.success("删除区域配置成功");
    }

    @PreAuthorize("@ss.hasPermi('zoneRule:applyZoneRule')")
    @Log(title = "区域配置管理", method = "应用区域配置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/applyZoneRule/{ids}")
    @ResponseBody
    @ApiOperation(value = "应用区域配置")
    public ResponseData applyZoneRule(@PathVariable List<Integer> ids) {
        if (ids == null || ids.size() < 1) {
            return ResponseData.error("请求参数有误");
        }
        zoneRuleService.applyZoneRule(ids);
        return ResponseData.success("应用区域配置成功");
    }

    /**
     * 获取库区对应层
     */
    @PostMapping(value = "/getDistFloor")
    @ResponseBody
    @ApiOperation(value = "获取库区对应层")
    public ResponseData getDistFloor() {
        Integer maxZ = nodeDBService.getMaxZ();
        List<Integer> floors = new ArrayList<>();
        for (Integer i = 1; i <= maxZ; i++) {
            floors.add(i);
        }
        return ResponseData.success(floors);
    }
}

